package com.situ.controller;

import com.situ.context.BaseContext;
import com.situ.domain.dto.CartDTO;
import com.situ.domain.vo.CartListVO;
import com.situ.service.CartService;
import com.situ.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车接口")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    /**
     * 添加购物车
     * @param cartDTO cartDTO.num 允许传递负数
     * @return
     */
    @PostMapping("/addItem")
    @Operation(summary = "添加购物车接口")
    public Result addItem(@RequestBody CartDTO cartDTO) {
        log.info("添加购物车:{}", cartDTO);
        cartService.addItem(cartDTO);
        return Result.ok("添加成功");
    }

    /**
     * 修改购物车中商品的数量
     * @param cartDTO
     * @return
     */
    @PostMapping("/changeNum")
    @Operation(summary = "修改购物车数量接口")
    public Result changeNum (@RequestBody CartDTO cartDTO) {
        log.info("修改购物车数量:{}", cartDTO);
        cartService.changeNum(cartDTO);
        return Result.ok("数量变更成功");
    }


    /**
     * 删除购物车中的商品
     * @param itemId
     * @return
     */
    @DeleteMapping("/deleteItem")
    @Operation(summary = "从购物车删除商品接口")
    public Result deleteItem(Integer itemId) {
        cartService.deleteItem(itemId);
        return Result.ok("删除成功");
    }


    /**
     * 查询购物车列表
     * @return
     */
    @Operation(summary = "查询购物车列表接口")
    @GetMapping("/getCartList")
    public Result<List<CartListVO>> getCartList() {
        Integer userId = BaseContext.getCurrentId();
        log.info("获取购物车列表,{}", userId);
            List<CartListVO> cartListVOS =  cartService.getCartListByUserId(userId);
        return Result.success(200, "获取购物车列表成功", cartListVOS);
    }




    /**
     * 根据ids查询订单接口
     * @param itemIds
     */
    @GetMapping("/getOrderListByIds")
    @Operation(summary = "根据ids查询订单接口")
    public Result<List<CartListVO>> getOrderListByIds(@RequestParam List<Integer> ids) {
        log.info("根据ids查询订单接口,{}", ids);
        List<CartListVO> orderListByIds = cartService.getOrderListByIds(ids);
        return Result.success(200, "查询成功", orderListByIds);
    }
}
